<?php
namespace app\admin\controller;

class Complaint extends Admin {
	public function __construct() {
		parent::__construct();
		$this->assign("model", "Complaint");
		$this->model = "Complaint";
	}
	public function get_where_map() {
		$status = input('param.status/d');
		$startdate = input('param.startdate/s');
		$enddate = input('param.enddate/s');
		$dealerid = input('param.dealerid/d');
		$keywords = input('param.keywords/s');
		$whereDatas = [];
		if (!is_null($status) && $status != -1) {
			$whereDatas['status'] = $status;
		}
		if ($dealerid) {
			$whereDatas['dealerid'] = $dealerid;
		} else {
			if ($this->uinfo['roleid'] == 4) {
				$dealerids = model('AdminUser')->where('puserid', $this->uinfo['userid'])->where('roleid', 6)->column('userid');
				$whereDatas['dealerid'] = ['in', $dealerids];
			} elseif ($this->uinfo['roleid'] == 6) {
				$whereDatas['dealerid'] = $this->uinfo['userid'];
			}
		}
		$starttime = $startdate ? strtotime($startdate) : 0;
		$endtime = $enddate ? strtotime($enddate) : 0;
		if ($starttime && $endtime) {
			$whereDatas['addtime'] = ['between', [$starttime, $endtime]];
		} elseif ($starttime && !$endtime) {
			$whereDatas['addtime'] = ['egt', $starttime];
		} elseif ($endtime && !$starttime) {
			$whereDatas['addtime'] = ['elt', $endtime];
		}
		if ($keywords) {
			$whereDatas['content|huifu'] = ['like', "%$keywords%"];
		}
		trace($whereDatas);
		return $whereDatas;
	}
	public function index() {
		$where['status'] = 0;
		if ($this->uinfo['roleid'] == 1) {
			$where['roleid'] = 6;
		} elseif ($this->uinfo['roleid'] == 4) {
			$where['roleid'] = 6;
			$where['puserid'] = $this->uinfo['userid'];
		} elseif ($this->uinfo['roleid'] == 6) {
			$where['userid'] = $this->uinfo['userid'];
		}
		$deallist = model('AdminUser')->where($where)->order('userid desc')->column('userid, username');

		$this->assign('deallist', $deallist);
		return $this->fetch();
	}
	public function adddesc() {
		$ids = input("id/d");
		$info = model($this->model)->where('id', $ids)->find();
		$this->assign('info', $info);
		$this->assign('id', input('param.id'));
		return $this->fetch();
	}

	public function add_huifu() {
		$postData = input("post.postData");
		parse_str($postData, $dataarr);
		if (!$dataarr['huifu']) {
			return json(['status' => 0, 'msg' => '请输入回复内容']);
		}
		$ids = model($this->model)->where('id', $dataarr['id'])->update(['adminid' => $this->uinfo['userid'], 'huifu' => $dataarr['huifu'], 'status' => 1, 'updatetime' => time()]);
		if ($ids === false) {
			return json(['status' => 0, 'msg' => '操作失败']);
		}
		return json(['status' => 1, 'msg' => '操作成功']);
	}

	public function image() {
		$id = input('param.id');
		$ids = explode(',', $id);
		$where = '';
		foreach ($ids as $value) {
			if ($value) {
				$where = 'id=' . $value . ' or ' . $where;
			}
		}
		$newwhere = rtrim($where, " or ");
		$list = model('Accessory')->where($newwhere)->select();
		$this->assign('urllist', $list);
		return $this->fetch();
	}
}